import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import { RouterProvider } from './contexts/RouterContext';
import { generateRoutes } from './utils/routerGenerator';
import { useRouter } from './contexts/RouterContext';
import { auth } from './utils/auth';
import Login from './pages/Login';
import MainLayout from './components/Layout';

// 受保护的路由组件
const ProtectedRoutes: React.FC = () => {
  const { routes } = useRouter();
  return <MainLayout>{generateRoutes(routes)}</MainLayout>;
};

const AppContent: React.FC = () => {
  const isLoggedIn = auth.isLoggedIn();
  
  if (!isLoggedIn) {
    return <Login />;
  }
  
  return <ProtectedRoutes />;
};

const App: React.FC = () => {
  return (
    <Router>
      <RouterProvider>
        <AppContent />
      </RouterProvider>
    </Router>
  );
};

export default App;